package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.SaleChanceMapper;
import com.woniuxy.dao.SalePlanMapper;
import com.woniuxy.pojo.SaleChance;
import com.woniuxy.pojo.SalePlan;
import com.woniuxy.pojo.User;
import com.woniuxy.service.SaleChanceService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.vo.SaleChanceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SaleChanceServiceImpl implements SaleChanceService {
    @Autowired
    private SaleChanceMapper saleChanceMapper;
    @Autowired
    private SalePlanMapper salePlanMapper;
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    /**
     * 查询所有的销售机会
     * 就是用在搜索功能那里
     * @return
     */
    @Override
    public DataGridView findAllSaleChance(SaleChanceVo saleChanceVo) {
        Page<Object> page= PageHelper.startPage(saleChanceVo.getPage(),saleChanceVo.getLimit());
        List<SaleChance> saleList=saleChanceMapper.selectAllSaleChance(saleChanceVo);
        return new DataGridView(page.getTotal(),saleList);//传递总数和数据
    }

    @Override
    public boolean addSaleChance(SaleChance saleChance) {
        boolean state=false;
        int i=saleChanceMapper.insertSaleChance(saleChance);//如果成功插入一条销售机会，那么会返回“1”，所以我们利用这个1判断
        if (i>0){
            state=true;
        }
        return state;
    }

    @Override
    public boolean removeSaleChance(int chanNo) {
        boolean state=false;
        int i=saleChanceMapper.deleteSaleChance(chanNo);
        if (i>0){
            state=true;
        }
        return state;
    }

    @Override
    public boolean updateSaleChance(SaleChance saleChance) {
        boolean state=false;
        int i=saleChanceMapper.updateSaleChance(saleChance);
        if (i>0){
            state=true;
        }
            return state;
    }

    /**
     * 查询所有未指派的销售机会和只要该客户经理登录时才可以看见
     * @param saleChanceVo
     * @return
     */
    @Override
    public DataGridView findAllSaleChanceByChanStatusAndChanDueTo(SaleChanceVo saleChanceVo) {
        logger.debug(saleChanceVo.getChanDueTo()+"impl进来了没？？？");
        Page<Object> page= PageHelper.startPage(saleChanceVo.getPage(),saleChanceVo.getLimit());
        List<SaleChance> saleList=saleChanceMapper.selectAllSaleChanceByChanStatus(saleChanceVo);
        return new DataGridView(page.getTotal(),saleList);//传递分页总数和数据
    }

    /**
     * 根据销售机会编号查询开发计划
     * @param chanNo
     * @return
     */
    @Override
    public List<SalePlan> findDevPlanByChanNo(int chanNo) {
        return salePlanMapper.selectSalePlanByCondition(chanNo);
    }

    /**
     * 保存插入开发机会
     * @param salePlan
     * @return
     */
    @Override
    public boolean saveDevPlan(SalePlan salePlan) {
        boolean state=false;
        int i=salePlanMapper.insertDevPlan(salePlan);
        //logger.info(salePlan.getPlanNo()+"@@@@@@@");
        if (i>0){
            state=true;
        }
        return state;
    }

    @Override
    public boolean updateDevPlan(SalePlan salePlan) {
        boolean state=false;
        int i=salePlanMapper.updateDevPlan(salePlan);
        if (i>0){
            state=true;
        }
        return state;
    }
    //按状态查询销售机会列表
    @Override
    public DataGridView selectSaleChanceByChanStatus(int chanStatus) {
        List<SaleChance> saleChances = saleChanceMapper.selectSaleChanceByChanStatus(chanStatus);
        return new DataGridView(Long.valueOf(saleChances.size()),saleChances);
    }
    //按状态查询销售机会个数
    @Override
    public SaleChance selectcountByChanStatu(int chanStatus) {
        SaleChance saleChance = saleChanceMapper.selectcountByChanStatu(chanStatus);
        return saleChance;
    }

    /**
     * 查询未指派的销售机会
     * @return
     */
    @Override
    public List<SaleChance> selectNoAssignSale() {
        return saleChanceMapper.selectNoAssignSale();
    }

    /**
     * 查询未指派的销售机会
     * @param saleChanceVo
     * @return
     */
    @Override
    public DataGridView noAssignSaleChance(SaleChanceVo saleChanceVo) {
        Page<Object> page= PageHelper.startPage(saleChanceVo.getPage(),saleChanceVo.getLimit());
        List<SaleChance> saleList=saleChanceMapper.noAssignSaleChance(saleChanceVo);
        return new DataGridView(page.getTotal(),saleList);//传递总数和数据
    }

    @Override
    public DataGridView selectNoSuccessDevPlan(SaleChanceVo saleChanceVo) {
        Page<Object> page= PageHelper.startPage(saleChanceVo.getPage(),saleChanceVo.getLimit());
        List<SaleChance> saleList=saleChanceMapper.selectNoSuccessDevPlan(saleChanceVo);
        return new DataGridView(page.getTotal(),saleList);//传递总数和数据
    }
}
